package com.dz.finance.wechat.factory;


import com.dz.finance.wechat.enums.WeChatMessageEnum;
import com.dz.finance.wechat.service.WeChatMessageStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class WeChatMessageStrategyFactory implements InitializingBean {


    @Resource
    private List<WeChatMessageStrategy> weChatMessageStrategyList;

    private Map<WeChatMessageEnum, WeChatMessageStrategy> map = new HashMap<>();

    public  WeChatMessageStrategy getStrategyByMsgType(String msgType) {
        WeChatMessageEnum weChatMessageEnum =  WeChatMessageEnum.getOne(msgType);
        return map.get(weChatMessageEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WeChatMessageStrategy weChatMessageStrategy : weChatMessageStrategyList) {
            map.put(weChatMessageStrategy.getTypeMsg(),weChatMessageStrategy);
        }
    }
}
